package com.heihei.transvideo;

import com.heihei.config.ConfigProperties;
import com.heihei.util.FileUtilF;
import com.heihei.util.StrUtilF;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

/**
 *  切分视频文件
 *
 * @author dhf
 * 2022年12月03日 17:41:31
 */
public class SplitVideoUtil {
    public static void main(String[] args) throws IOException {
        ConfigProperties.updateProperties();
        start();
    }

    public static void start() throws IOException {
        String line = null;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入文件绝对路径：");
        String fileAbsPath = sc.nextLine();
        File file = new File(fileAbsPath);
        String fileName = file.getName();
        File targetDir = file.getParentFile();
        System.out.println("请输入时间，中间用回车隔开，以end结束");
        int index = 1;
        line = sc.nextLine();
        StringBuilder sb = new StringBuilder();
        while (!"end".equals(line)) {
            String newLine = line.trim();
            String[] arr = newLine.split(" ");
            int i = 0;
            String[] timeStr = new String[3];
            int j = 0;
            for (; j < arr.length; j++) {
                String s = arr[j];
                String trim = s.trim();
                if(trim != null && !"".equals(trim)) {
                    timeStr[i] = trim;
                    i++;
                }
                if(i >= 3) {
                    break;
                }
            }
            String command = "\"" + ConfigProperties.FFMPEG_HOME_DIR + "\" -i \"" + fileName + "\" -ss " + timeStr[0] + " -to " + timeStr[1] + "  -c:v libx264 \"temp/" + StrUtilF.getNumberString(index, 2);
            sb.append(command);
            if(timeStr[2] != null && !"".equals(timeStr[2])) {
                sb.append("_");
                for (int k = j; k < arr.length; k++) {
                    sb.append(arr[k]);
                    if(k != arr.length - 1) {
                        sb.append(" ");
                    }
                }
            }
            sb.append(".mp4").append("\"\r\n");
            index++;
            line = sc.nextLine();
        }
//        System.out.println("pause");
        sb.append("pause");
        FileUtilF.writeTxtFile(sb.toString(), new File(targetDir, "start_split_video.cmd"), "GBK");
    }
}
